<template>
  <div class="device-list-container">
    <search-form
      v-model="searchForm"
      @search="search"
      @sendList="sendList"
      @receiveList="receiveList"
    ></search-form>
    <div class="device-list-data-table">
      <data-table
        v-model="sort"
        :tableData="tableData"
        @search="search"
        @orderBy="order"
        @select="getDeviceList"
        @send="sendDevice"
        @receive="receiveDevice"
      ></data-table>
    </div>
    <div
      align="right"
      class="paging-footer"
    >
      <el-pagination
        background
        @size-change="pageSizeChangeHandler"
        @current-change="currentPageChangeHandler"
        :current-page.sync="currentPage"
        :page-size.sync="pageSize"
        :page-sizes="pageSizes"
        layout="total,sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import DataTable from './dataTable';
import moment from 'moment';
import SearchForm from './searchForm';
import { MomentFormat, SortType } from '../../../../constants/common';
import { Message, MessageBox } from 'element-ui';
import { getDeviceListApi } from '../../../../api/device';
import { parseError } from '../../../../utils';

export default {
  name: 'DeviceList',
  components: {
    SearchForm,
    DataTable
  },
  data() {
    return {
      deviceList: [],
      orderBy: 'createdAt',
      searchForm: {
        btnLoading: false,
        typeUuid: '',
        factoryUuid: '',
        name: '',
        serialNumber: '',
        versionName: '',
        status: ''
      },
      sort: {
        sortType: SortType.DESC
      },
      active: true,
      pageSize: 10,
      totalCount: 0,
      tableData: [],
      currentPage: 1,
      pageSizes: [5, 10, 20, 50, 100]
    };
  },
  created() {
    this.search();
  },
  methods: {
    getDeviceList(value) {
      this.deviceList = value;
    },
    sendList() {
      if (this.deviceList.length == 0) return Message.warning('请选择设备');
      this.$emit('sendDeviceList', this.deviceList);
    },
    receiveList() {
      if (this.deviceList.length == 0) return Message.warning('请选择设备');
      this.$emit('receiveDeviceList', this.deviceList);
    },
    sendDevice(val) {
      this.$emit('sendDevice', val);
    },
    receiveDevice(val) {
      this.$emit('receiveDevice', val);
    },
    order(value) {
      this.orderBy = value;
    },
    search() {
      this.searchForm.btnLoading = true;
      getDeviceListApi(
        this.currentPage - 1,
        this.pageSize,
        this.sort.sortType,
        this.orderBy,
        this.searchForm.typeUuid,
        this.searchForm.factoryUuid,
        this.searchForm.name,
        this.searchForm.serialNumber,
        this.searchForm.status,
        this.searchForm.active
      )
        .then(({ total_count, data }) => {
          this.totalCount = total_count;
          this.tableData = data.map(device => {
            return {
              uuid: device.uuid,
              typeName: device.type_name,
              typeUuid: device.type_uuid,
              factoryName: device.factory_name || null,
              factoryUuid: device.factory_uuid || null,
              name: device.name,
              serialNumber: device.serial_number,
              latitude: device.latitude,
              longitude: device.longitude,
              location: device.location,
              status: device.status,
              createdAt: moment(device.created_at).format(MomentFormat),
              updatedAt: moment(device.updated_at).format(MomentFormat),
              dataList: device.data_list || null,
              active: device.active
            };
          });
        })
        .catch(error => Message.error(parseError(error)))
        .finally(() => (this.searchForm.btnLoading = false));
    },
    pageSizeChangeHandler() {
      this.currentPage = 1;
      this.search();
    },
    currentPageChangeHandler() {
      this.search();
    }
  }
};
</script>

<style lang="scss">
.device-list-container {
  // margin: 10px;
  .el-button--primary {
    background-color: #2c83b1 !important;
    border-color: #2c83b1 !important;
  }
  .device-list-data-table {
    background-color: #fff;
    padding: 10px;
    margin-top: 10px;
    margin: 10px 10px 0 10px;
  }
}
.paging-footer {
  margin: 0 10px;
  padding: 10px;
  background: #fff;
}
</style>

